{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 数字图像处理大作业\n",
    "\n",
    "### 一、先提取.mat文件中的数据:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(2048, 820, 4)\n",
      "[-0.03387079  0.          0.          0.        ]\n"
     ]
    }
   ],
   "source": [
    "import scipy.io as scio\n",
    "from scipy.fft import fft,ifft\n",
    "import matplotlib.pyplot as plt\n",
    "datapath = \"./spect.mat\"\n",
    "data = scio.loadmat(datapath)\n",
    "# print(data.keys())\n",
    "# 最后的输出的结果为:\n",
    "# dict_keys(['__header__', '__version__', '__globals__', 'spect'])\n",
    "DataSpect = data['spect']\n",
    "# print(DataSpect)\n",
    "print(DataSpect.shape)\n",
    "print(DataSpect[0][0])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2、对数据进行处理："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'DataSpect' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "Input \u001b[0;32mIn [5]\u001b[0m, in \u001b[0;36m<cell line: 3>\u001b[0;34m()\u001b[0m\n\u001b[1;32m      1\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mnumpy\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m \u001b[38;5;21;01mnp\u001b[39;00m\n\u001b[1;32m      2\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mcv2\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m \u001b[38;5;21;01mcv\u001b[39;00m\u001b[38;5;66;03m# retval = cv.imread('./WechatIMG46173.jpeg')\u001b[39;00m\n\u001b[0;32m----> 3\u001b[0m r,g,b,d \u001b[38;5;241m=\u001b[39m cv\u001b[38;5;241m.\u001b[39msplit(\u001b[43mDataSpect\u001b[49m)\n\u001b[1;32m      4\u001b[0m h,w \u001b[38;5;241m=\u001b[39m r\u001b[38;5;241m.\u001b[39mshape[:\u001b[38;5;241m2\u001b[39m]\n\u001b[1;32m      7\u001b[0m \u001b[38;5;66;03m# print(r.shape)\u001b[39;00m\n\u001b[1;32m      8\u001b[0m \u001b[38;5;66;03m# print(g.shape)\u001b[39;00m\n\u001b[1;32m      9\u001b[0m \u001b[38;5;66;03m# print(b.shape)\u001b[39;00m\n\u001b[0;32m   (...)\u001b[0m\n\u001b[1;32m     25\u001b[0m \u001b[38;5;66;03m# 从这个最后的结果我们可以发现，是存在一个平移的过程,我们创建四个矩阵来进行变换；\u001b[39;00m\n\u001b[1;32m     26\u001b[0m \u001b[38;5;66;03m# 进行仿射变换中的平移变换；\u001b[39;00m\n",
      "\u001b[0;31mNameError\u001b[0m: name 'DataSpect' is not defined"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "import cv2 as cv# retval = cv.imread('./WechatIMG46173.jpeg')\n",
    "r,g,b,d = cv.split(DataSpect)\n",
    "h,w = r.shape[:2]\n",
    "\n",
    "\n",
    "# print(r.shape)\n",
    "# print(g.shape)\n",
    "# print(b.shape)\n",
    "# print(d.shape)\n",
    "# -----------------\n",
    "# 最终的输出结果为：\n",
    "    # (2048, 820)\n",
    "    # (2048, 820)\n",
    "    # (2048, 820)\n",
    "    # (2048, 820)\n",
    "\n",
    "\n",
    "# print(rifft)使用print()函数输出后,我们可以发现我们得到了实部的信息;\n",
    "# ----------------------\n",
    "# print(r[0][:20])\n",
    "# print(g[0][:20])\n",
    "# print(b[0][:20])\n",
    "# print(d[0][:20])\n",
    "# 从这个最后的结果我们可以发现，是存在一个平移的过程,我们创建四个矩阵来进行变换；\n",
    "# 进行仿射变换中的平移变换；\n",
    "MOne = np.float32([[1,0,0],[0,1,0]])\n",
    "MTwo = np.float32([[1,0,-3],[0,1,0]])\n",
    "MThree = np.float32([[1,0,-6],[0,1,0]])\n",
    "MFour = np.float32([[1,0,-9],[0,1,0]])\n",
    "\n",
    "\n",
    "# 下面使用Cv的warpAffine(img,M,(w,h))来进行平移的操作;\n",
    "rMove = cv.warpAffine(r,MOne,(w,h)) \n",
    "gMove = cv.warpAffine(g,MTwo,(w,h)) \n",
    "bMove = cv.warpAffine(b,MThree,(w,h)) \n",
    "dMove = cv.warpAffine(d,MFour,(w,h))\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3、显示图像的部分："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 显示图像的部分:\n",
    "cv.namedWindow(\"r\")\n",
    "\n",
    "cv.imshow('r',g)\n",
    "cv.waitKey()\n",
    "cv.destroyAllWindows()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.10.7 64-bit",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.7 (main, Sep 14 2022, 22:38:23) [Clang 14.0.0 (clang-1400.0.29.102)]"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "b0fa6594d8f4cbf19f97940f81e996739fb7646882a419484c72d19e05852a7e"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
